//
//  KALoginVC.m
//  KeepAccounts
//
//  Created by honeyrr on 2017/11/8.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "KALoginVC.h"
#import "KARegistVC.h"
#import "KASMSLoginViewController.h"

@interface KALoginVC ()

@property (weak, nonatomic) IBOutlet UITextField *accountTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *registBtn;

@end

@implementation KALoginVC


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"登录";
    [self setUpUI];
}

-(void) setUpUI
{
    _imgView.layer.cornerRadius = 20;
    _imgView.layer.masksToBounds = YES;
    
    [self setTFLeftImageViewName:@"username" toTF:_accountTF];
    [self setTFLeftImageViewName:@"password" toTF:_passwordTF];
    
    CGFloat btnRadius = 10;
    _loginBtn.layer.cornerRadius = btnRadius;
    _loginBtn.layer.masksToBounds = YES;
    _registBtn.layer.cornerRadius = btnRadius;
    _registBtn.layer.masksToBounds = YES;
    
    _registBtn.layer.borderWidth = 1;
    _registBtn.layer.borderColor = KANavTabColor.CGColor;
}

-(void) setTFLeftImageViewName:(NSString *)imageName toTF:(UITextField *)tf
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 44)];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
    imageView.center = view.center;
    [view addSubview:imageView];
    tf.leftViewMode = UITextFieldViewModeAlways;
    tf.leftView = view;
}
- (IBAction)login:(id)sender {
    NSArray *arr = [[NSUserDefaults standardUserDefaults] valueForKey:@"users"];
    NSMutableArray *marr = [NSMutableArray arrayWithArray:arr];
    if ([_accountTF.text isEqualToString:@""] && [_passwordTF.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请填写完信息"];
    } else {
        BOOL isSame = false;
        for (NSDictionary *dict in marr) {
            if ([_accountTF.text isEqualToString:dict[@"userName"]] && [_passwordTF.text isEqualToString:dict[@"password"]]) {
                isSame = true;
            }
        }
        if (isSame) {
            [SVProgressHUD showSuccessWithStatus:@"登录成功"];
            [self.navigationController popViewControllerAnimated:YES];
            [[NSUserDefaults standardUserDefaults] setObject:_accountTF.text forKey:@"userName"];
        } else {
            [SVProgressHUD showErrorWithStatus:@"登录失败"];
        }
    }
}
- (IBAction)regist:(id)sender {
    [self.navigationController pushViewController:[KARegistVC new] animated:YES];
}
- (IBAction)SMSLogin:(id)sender {
    [self.navigationController pushViewController:[KASMSLoginViewController new] animated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
@end
